package cn.stylefeng.guns.modular.hp.controller;

import cn.stylefeng.guns.config.properties.HpProperties;
import cn.stylefeng.guns.core.common.annotion.BussinessLog;
import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.modular.hp.domain.ExpertInfoDO;
import cn.stylefeng.guns.modular.hp.model.ExpertInfo;
import cn.stylefeng.guns.modular.hp.model.PageContentInfo;
import cn.stylefeng.guns.modular.hp.service.IExpertInfoService;
import cn.stylefeng.guns.modular.hp.service.IPageContentInfoService;
import cn.stylefeng.guns.modular.hp.warpper.ExpertInfoWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 专家介绍控制器
 *
 * @author xzqiang
 * @date 2018-12-12
 */
@Controller
@RequestMapping("/hp/expert")
public class ExpertInfoController extends BaseController {

    private String PREFIX = "/hp/expert/";

    @Autowired
    private HpProperties hpProperties;
    
    @Autowired
    private IExpertInfoService expertInfoService;
    
    @Autowired
    private IPageContentInfoService pageContentInfoService;
    
    /**
     * 跳转到专家介绍列表首页
     */
    @RequestMapping("")
    public String index() {
    	String htmlPath = "expertInfo.html";
        return PREFIX + htmlPath;
    }

    /**
     * 跳转到添加专家介绍
     */
    @RequestMapping("/expertInfo_add")
    public String expertInfoAdd() {
    	String htmlPath = "expertInfo_add.html";
        return PREFIX + htmlPath;
    }

    /**
     * 跳转到修改专家介绍
     */
    @RequestMapping("/expertInfo_update/{infoId}")
    public String expertInfoUpdate(@PathVariable String infoId, Model model) {
    	ExpertInfo info = this.expertInfoService.selectById(infoId);
    	PageContentInfo pageContent = this.pageContentInfoService.selectById(info.getPageContentId());
    	
    	ExpertInfoDO infoDO = new ExpertInfoDO();
    	
    	infoDO.setId(info.getId());
    	infoDO.setName(info.getName());
    	infoDO.setRank(info.getRank());
    	infoDO.setPicName(info.getPicName());
    	infoDO.setPicUrl(info.getPicUrl());
    	infoDO.setContent(pageContent.getContent());
    	infoDO.setPri(info.getPri());
    	
        model.addAttribute("info", infoDO);
        LogObjectHolder.me().set(info);

        String htmlPath = "expertInfo_edit.html";
    	
        return PREFIX + htmlPath;
    }


    /**
     * 获取专家介绍列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Map<String, Object>> list = this.expertInfoService.list(condition);
        return super.warpObject(new ExpertInfoWrapper(list));
    }

    /**
     * 新增专家介绍
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @BussinessLog(value = "新增专家介绍", key = "id")//, dict = NoticeMap.class
    public Object add(ExpertInfoDO expertInfoDO) {
        if (ToolUtil.isOneEmpty(expertInfoDO, expertInfoDO.getName(),expertInfoDO.getRank(), expertInfoDO.getPri(), expertInfoDO.getPicUrl(), expertInfoDO.getContent())) { //, topMenuInfo.getPageContentId()
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        
        //新增页面内容
        PageContentInfo pageContent = new PageContentInfo();
        pageContent.setContent(expertInfoDO.getContent());
        pageContent.setCreater(ShiroKit.getUser().getId());
        pageContent.setCreatedTime(new Date());
        pageContent.insert();
        
        //新增专家介绍
        ExpertInfo newExpertInfo = new ExpertInfo();
        newExpertInfo.setName(expertInfoDO.getName());
        newExpertInfo.setRank(expertInfoDO.getRank());
        newExpertInfo.setPicName(expertInfoDO.getPicUrl());
        newExpertInfo.setPicUrl(expertInfoDO.getPicUrl());
        newExpertInfo.setPageContentId(pageContent.getId());
        newExpertInfo.setPri(expertInfoDO.getPri());
        newExpertInfo.setCreater(ShiroKit.getUser().getId());
        newExpertInfo.setCreatedTime(new Date());
        newExpertInfo.insert();
        
        return SUCCESS_TIP;
    }

    /**
     * 删除专家介绍
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @BussinessLog(value = "删除专家介绍", key = "id")
    public Object delete(@RequestParam String id) {

        this.expertInfoService.deleteById(id);

        return SUCCESS_TIP;
    }

    /**
     * 修改专家介绍
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @BussinessLog(value = "修改专家介绍", key = "id")//, dict = NoticeMap.class
    public Object update(ExpertInfoDO expertInfoDO) {
    	if (ToolUtil.isOneEmpty(expertInfoDO, expertInfoDO.getName(),expertInfoDO.getRank(), expertInfoDO.getPri(), expertInfoDO.getPicUrl(), expertInfoDO.getContent())) { //, topMenuInfo.getPageContentId()
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
    	
    	ExpertInfo oldExpertInfo = this.expertInfoService.selectById(expertInfoDO.getId());
        PageContentInfo oldPageContent = this.pageContentInfoService.selectById(oldExpertInfo.getPageContentId());
        
        
        oldPageContent.setContent(expertInfoDO.getContent());
    	oldPageContent.updateById();
        
    	oldExpertInfo.setName(expertInfoDO.getName());
    	oldExpertInfo.setRank(expertInfoDO.getRank());
    	oldExpertInfo.setPicName(expertInfoDO.getPicUrl());
    	oldExpertInfo.setPicUrl(expertInfoDO.getPicUrl());
    	oldExpertInfo.setPri(expertInfoDO.getPri());
        oldExpertInfo.updateById();
        
        return SUCCESS_TIP;
    }

    /**
     * 跳转到分配科室页面
     */
    @RequestMapping("/department_assign/{expertInfoId}")
    public String departmentAssign(@PathVariable String expertInfoId, Model model) {
        if (ToolUtil.isEmpty(expertInfoId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        ExpertInfo expertInfo = this.expertInfoService.selectById(expertInfoId);

        model.addAttribute("expertInfoId", expertInfo.getId());
        return PREFIX + "expertInfo_department_assign.html";
    }

}
